/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.applet;

/**
 *
 * @author Mr.Sam
 */
import java.util.*;
import fr.ove.applet.AppletLoadingChecked;
import fr.ove.applet.ListenableApplet;
import fr.ove.applet.events.AppletListener;
import fr.ove.applet.events.AppletEvent;

/**
* This is an implementation of the <CODE>ListenableApplet</CODE> interface.<BR>
* This applet takes as argument a parameter named <CODE>name</CODE> which
* represents the name the instance to identify it for futher treatments and
* for registration.<BR>
* Maintains a list of listeners (<CODE>AppletListener</CODE>) of the instance
* and fires to them an event (<CODE>AppletEvent</CODE>) for each change.
* <P>
* This class is an final class, so it can't be overloaded.
*
* @author © 1999 DIRAT Laurent
* @version 1.0  30/04/99
*/
public final class AListenableApplet extends AppletLoadingChecked implements ListenableApplet {
    /**
    * The name of the applet.<BR>
    * We need to identify the instance for further treatments
    */
    private String name;

    /**
    * The list of listeners of the instance.
    */
    private Vector listeners = new Vector();

    public void init() {
        super.init();
        name = getParameter("name");
    }

    /**
    * Sets the name of the applet.
    * @param name the name of the applet.
    */
    public void setName(String name) {
        this.name = name;
    }

    /**
    * Returns the name of the applet.
    */
    public String getName() {
        return name;
    }

    /**
    * Registers a listener of the instance.
    */
    public void addAppletListener(AppletListener appletListener) {
        if (!listeners.contains(appletListener))
            listeners.addElement(appletListener);
    }

    /**
    * Unregisters the specified listener of the instance.
    * @param appletListener the listener to remove.
    */
    public void removeAppletListener(AppletListener appletListener) {
        listeners.removeElement(appletListener);
    }

    /**
    * Fires an event to all the registered listeners of the instance.
    * @param appletEvent the event to fire.
    */
    public void fireAppletEvent(AppletEvent appletEvent) {
        for (int i = 0; i < listeners.size(); i++)
          ((AppletListener) listeners.elementAt(i)).consumeAppletEvent(appletEvent);
    }
}
